package com.yixiang.np.config;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

//import tk.mybatis.mapper.common.Mapper;
//import tk.mybatis.mapper.mapperhelper.MapperHelper;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:platform.properties")
@ComponentScan(basePackages = {"com.yixiang.np.service", "com.yixiang.np.mapper"})
public class Configurator {

    private Logger logger = Logger.getLogger(Configurator.class);

    private
    @Value("${mysql.database.url}")
    String database_url;
    private
    @Value("${mysql.database.user}")
    String database_username;
    private
    @Value("${mysql.database.password}")
    String database_password;

    @Autowired
    private DataSource dataSource; 

    @Autowired
    private SqlSessionFactory sqlSessionFactory; 

    @Bean
    public DataSource dataSource() {
        try {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setUrl(database_url);
            dataSource.setUsername(database_username);
            dataSource.setPassword(database_password);
            dataSource.setInitialSize(1);
            dataSource.setMinIdle(1);
            dataSource.setMaxActive(20);
            dataSource.setMaxWait(60000);
            dataSource.setTimeBetweenEvictionRunsMillis(60000);
            dataSource.setMinEvictableIdleTimeMillis(300000);
            dataSource.setValidationQuery("select 'x' from dual");
            dataSource.setTestWhileIdle(true);
            dataSource.setTestOnBorrow(false);
            dataSource.setTestOnReturn(false);
            dataSource.setPoolPreparedStatements(false);
            dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
            dataSource.setFilters("stat");
            return dataSource;
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() {
        try {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setConfigLocation(new ClassPathResource("mybatis/Config.xml"));
            return factoryBean.getObject();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    @Bean
    public MapperHelper mapperHelper() {
        MapperHelper mapperHelper = new MapperHelper();
        Properties properties = new Properties();
        properties.setProperty("ORDER", "AFTER");
        mapperHelper.setProperties(properties);
//        tk.mybatis.mapper.entity.Config config = new tk.mybatis.mapper.entity.Config();
//        mapperHelper.setConfig(config);
        mapperHelper.registerMapper(Mapper.class);
        mapperHelper.processConfiguration(sqlSessionFactory.getConfiguration());
        return mapperHelper;
    }

    @Bean
    public DataSourceTransactionManager dsTransactionManager() {
        DataSourceTransactionManager dsTransactionManager = new DataSourceTransactionManager();
        dsTransactionManager.setDataSource(dataSource);
        return dsTransactionManager;
    }
      
}
